const { join } = require('path')
const moment = require('moment')
const { URL } = require('url')
const { getRepoTreeList, getRepoBlobHighLisgt } = require("../api/repo");
const Contants = require('../contants');
const { NotFoundExecption } = require('../util/exception');

module.exports = class RepoController {
    // 前置上下文赋值
    prevContext(ctx) {
        const url = new URL(Contants.git_http_path)
        url.pathname = join(url.pathname, ctx.user.username, `${ctx.repo.name}.git`)
        ctx.repo.git_http_path = url.href
    }
    /**
     * @private 
     * 这个函数时用来为公共上下文赋值的
     */
    publicContext(ctx, commit_set, branchArray, branch, profile) {
        const latest_updated_date = new Date(commit_set[0].time)
        const latest_commit = {
            updated_time: `${latest_updated_date.getFullYear()}-${latest_updated_date.getMonth() + 1}-${latest_updated_date.getDate()} ${latest_updated_date.getHours()}:${latest_updated_date.getMinutes()}:${latest_updated_date.getSeconds()}`,
            relative_time: moment(latest_updated_date).fromNow(),
            comment: commit_set[0].body,
            hash: commit_set[0].commitHash.substring(0, 6),
            username: commit_set[0].username,
            total: commit_set.length,
        }

        ctx.repo.latest_commit = latest_commit
        ctx.repo.branchArray = branchArray
        ctx.repo.branch = branch
        ctx.repo.profile = profile
    }

    async repo(ctx) {
        this.prevContext(ctx)
        // ctx.repo.branch可能存在也可能时undefined
        const treeRes = await getRepoTreeList(ctx.user.username, ctx.repo.name, ctx.repo.branch, ctx.repo.path)
        if (treeRes.code !== 20000) {
            throw new NotFoundExecption()
        }
        // 如果是空仓库直接返回
        if (treeRes.data.isEmptyRepo) {
            ctx.repo.is_empty_repo = true
            return ;
        }
        /**
         * @example
         * list = {
               type: 'blob',
                hash: '4b3a37c986fc1da3da2f7befc48c8064ff0c993a',
                name: 'package.json',
                username: 'Tsdy',
                updated_time: 'Wed Dec 8 11:55:09 2021 +0800',
                comment: 'init'
            }
         */
        const list = treeRes.data.list
        const commit_set = treeRes.data.commit_set
        const branchArray = treeRes.data.branchArray
        const profile = treeRes.data.profile
        // 实际返回的分支
        const branch = treeRes.data.branch
        ctx.repo.fileList = list.map(item => {
            const date = new Date(item.updated_time)
            item.url = join(ctx.repo.path, item.name)
            item.relative_time = moment(date).fromNow()
            return item
        })
        this.publicContext(ctx, commit_set, branchArray, branch, profile)
    }

    async blob(ctx) {
        this.prevContext(ctx)
        const blobRes = await getRepoBlobHighLisgt(ctx.user.username, ctx.repo.name, ctx.repo.branch, ctx.repo.path)
        if (blobRes.code !== 20000) {
            return ;
        }
        const codeLines = blobRes.data.codeLines
        
        const commit_set = blobRes.data.commit_set
        const branchArray = blobRes.data.branchArray
        const profile = blobRes.data.profile
        // 实际返回的分支
        const branch = blobRes.data.branch
        ctx.codeLines = codeLines
        this.publicContext(ctx, commit_set, branchArray, branch, profile)
    }

    
}